package leetcode.f1t100;

import leetcode.struct.ListNode;

/**
 * 清理链表中出现过重复的所有值
 * https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/
 *
 * @author lichx
 * @createTime 2024/1/29 15:25
 */
public class Q82_RemoveDuplicatesFormSortedListII {

    public static void main(String[] args) {
        ListNode listNode = new ListNode(1, new ListNode(1, new ListNode(2)));
        Q82_RemoveDuplicatesFormSortedListII bean = new Q82_RemoveDuplicatesFormSortedListII();
        ListNode result = bean.deleteDuplicates(listNode);
        while(result!= null) {
            System.out.println(result.val);
            result = result.next;
        }
    }

    public ListNode deleteDuplicates(ListNode head) {
        ListNode lead = new ListNode(), cur = lead;
        while(head != null) {
            if(head.next != null && head.val == head.next.val) {
                while(head.next != null){
                    if(head.next.val != head.val){
                        break;
                    }
                    head = head.next;
                }
                head = head.next;
            } else {
                cur.next = head;
                head = head.next;
                cur = cur.next;
                cur.next = null;
            }
        }
        return lead.next;
    }
}
